/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projekt;

/**
 *
 * @author linuslofberg
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Start class is used as a start screen that players enter their hero names
 * into.
 */
public final class Start extends JFrame {

    /**
     * String that stores the player name.
     */
    private String player;
    /**
     * X width used.
     */
    private final int dimX = 600;
    /**
     * Y width used.
     */
    private final int dimY = 500;
    /**
     * Stored dungeon being used.
     */
    private Dungeon dg;
    /**
     * Image used for background.
     */
    private BufferedImage img;

    /**
     * Constructor for the class sets up the JFrame and starts the text field.
     * When name is enters sends the name to the dungeon which turns visible and
     * sets up the level.
     *
     * @param dgx Dungeon used.
     */
    public Start(final Dungeon dgx) {
        dg = dgx;
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (dim.width - dimX) / 2;
        int y = (dim.height - dimY) / 2;

        setBackground(Color.BLACK);
        setAlwaysOnTop(true);
        setResizable(false);
        setSize(dimX, dimY);
        setLocation(x, y);
        toFront();

        try {
            img = ImageIO.read(new File("src/projekt/Image/castle1.png"));
        } catch (IOException e) {
            System.out.println("file not found");
        }



        JPanel startPanel = new JPanel();
        JLabel label = new JLabel("Select your name:");
        JLabel jimg = new JLabel(new ImageIcon(img));
        final JTextField text = new JTextField(15);
        label.setForeground(Color.white);

        startPanel.setBackground(Color.BLACK);
        startPanel.add(label);
        startPanel.add(text);
        startPanel.add(jimg);

        this.add(startPanel);
        text.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                if (text.getText() != null) {
                    player = text.getText();
                    dg.trueStart(player);
                    dispose();
                }
            }
        });
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        repaint();

    }
}
